// ===================================
// <copyright>
// This file is part of LABSMFController.
//
//    LABSMFController is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    LABSMFController is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with LABSMFController.  If not, see <http://www.gnu.org/licenses/>.
//    Copyright 2011 David Bakker
//
//</copyright>
// <author>David Bakker</author>
// <email>mailto:DavidFBakker@gmail.com</email>
// <created>Sunday, May 08, 2011</created>
// <lastedit>Sunday, May 08, 2011</lastedit>
// ===================================

using System;
using System.IO;
using GHIElectronics.NETMF.IO;
using LABSMFController.LABS.Classses;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;

namespace LABSMFController.LABS
{
    public static class SDCard
    {
        public static Setting[] GetSettings()
        {
            var test = new Setting[1];
            try
            {
                var sdPs = new PersistentStorage("SD");

                // Mount the file system
                sdPs.MountFileSystem();


                Debug.Print("Getting files and folders:");
                if (VolumeInfo.GetVolumes()[0].IsFormatted)
                {
                    string rootDirectory =
                        VolumeInfo.GetVolumes()[0].RootDirectory;

                    var fileHandle = new FileStream(rootDirectory + @"\Config.txt", FileMode.Open);

                    const int bufferSize = 10*1024;

                    var data = new byte[bufferSize];

                    int read = fileHandle.Read(data, 0, bufferSize);
                    fileHandle.Close();
                    fileHandle.Dispose();

                    var input = (Setting[]) Reflection.Deserialize(data, typeof (Setting[]));

                    int c;
                    for (c = 0; c < input.Length; c++)
                    {
                        if (input[c] == null) break;
                    }
                    test = new Setting[c];
                    for (int d = 0; d < c; d++)
                    {
                        if (input[d] == null) break;

                        test[d] = Settings.CopySetting(input[d]);
                    }
                }
                else
                {
                    Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
                }
                // Unmount
                sdPs.UnmountFileSystem();
                sdPs.Dispose();
            }
            catch (Exception ex)
            {
                string err = ex.Message;
            }
            return test;
        }


        public static void WriteSettings()
        {
            try
            {
                var sdPs = new PersistentStorage("SD");

                // Mount the file system
                sdPs.MountFileSystem();

                Debug.Print("Getting files and folders:");
                if (VolumeInfo.GetVolumes()[0].IsFormatted)
                {
                    string rootDirectory =
                        VolumeInfo.GetVolumes()[0].RootDirectory;

                    var fileHandle = new FileStream(rootDirectory + @"\Config.txt", FileMode.Create);

                    byte[] data = Reflection.Serialize(Settings.BeerSettings, typeof (Setting[]));
                    fileHandle.Write(data, 0, data.Length);
                    fileHandle.Close();
                }
                else
                {
                    Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
                }
                // Unmount
                sdPs.UnmountFileSystem();
                sdPs.Dispose();
            }
            catch (Exception)
            {
            }
        }

        public static void Start()
        {
            // ...
            // SD Card is inserted
            // Create a new storage device
            var sdPs = new PersistentStorage("SD");

            // Mount the file system
            sdPs.MountFileSystem();


            // Assume one storage device is available, access it through 
            // Micro Framework and display available files and folders:
            Debug.Print("Getting files and folders:");
            if (VolumeInfo.GetVolumes()[0].IsFormatted)
            {
                string rootDirectory =
                    VolumeInfo.GetVolumes()[0].RootDirectory;
                string[] files = Directory.GetFiles(rootDirectory);
                string[] folders = Directory.GetDirectories(rootDirectory);

                Debug.Print("Files available on " + rootDirectory + ":");
                for (int i = 0; i < files.Length; i++)
                    Debug.Print(files[i]);

                Debug.Print("Folders available on " + rootDirectory + ":");
                for (int i = 0; i < folders.Length; i++)
                    Debug.Print(folders[i]);


                var fileHandle = new FileStream(rootDirectory +
                                                @"\hello.txt", FileMode.Create);

                byte[] data = Reflection.Serialize(Settings.BeerSettings, typeof (Setting));

                fileHandle.Write(data, 0, data.Length);
                fileHandle.Close();
            }
            else
            {
                Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
            }


            // Unmount
            sdPs.UnmountFileSystem();
        }
    }
}